﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using TSharp.Core.Aop.Handler;

namespace TSharp.Core.Aop.Attributes
{
	/// <summary>
	/// 异常的定制特性
	/// </summary>
	public class ExceptionAttribute : HandlerAttribute
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ExceptionAttribute"/> class.
		/// </summary>
		/// <param name="messageTemplate">The message template.</param>
		/// <param name="policyName">Name of the policy.</param>
		public ExceptionAttribute(string messageTemplate, string policyName)
		{
			MessageTemplate = messageTemplate;
			PolicyName = policyName;
			Order = 5;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ExceptionAttribute"/> class.
		/// </summary>
		/// <param name="messageTemplate">The message template.</param>
		public ExceptionAttribute(string messageTemplate)
			: this(messageTemplate, GolbalPolicyName)
		{
		}

		/// <summary>
		/// 全局错误配置名称
		/// </summary>
		public static string GolbalPolicyName { get; set; }

		/// <summary>
		/// 错误消息模板
		/// </summary>
		public string MessageTemplate { get; set; }

		/// <summary>
		/// Policy名称
		/// </summary>
		public string PolicyName { get; set; }

		/// <summary>
		/// Derived classes implement this method. When called, it
		/// creates a new call handler as specified in the attribute
		/// configuration.
		/// </summary>
		/// <param name="container">The <see cref="T:Microsoft.Practices.Unity.IUnityContainer"/> to use when creating handlers,
		/// if necessary.</param>
		/// <returns>A new call handler object.</returns>
		public override ICallHandler CreateHandler(IUnityContainer container)
		{
			return new ExceptionCallHandler(MessageTemplate, PolicyName, Order);
		}
	}
}